#include "wds_object.h"

//WDSObject *WDSObject::mViewport = 0;


WDSObject::WDSObject()
{
	mMoveSpeed = mRotation  = 0;	
	mCurrentCollideObject = NULL;
}

void WDSObject::init(float pX,float pY,float pWidth,float pHeight,int pLimitX,int pLimitY,float pMoveSpeed)
{
	setWidth(pWidth);
	setHeight(pHeight);
	setX(pX);
	setY(pY);
	mMoveSpeed = pMoveSpeed;
	mLimitX = pLimitX;
	mLimitY = pLimitY; 
}


bool WDSObject::getIsInsideScreen() const
{
	//if(mViewport == 0)return false;
	

	return true;
}

bool WDSObject::getIsNearScreen() const
{
	//if(mViewport == 0)return false;
	//if(mLeft > mViewport->getRight() + mViewport->getHalfWidth() ) return false;
	//if(mTop >mViewport->getBottom() + mViewport->getHalfHeight() ) return false;
	//if(mRight  < mViewport->getLeft() -  mViewport->getHalfWidth() ) return false;
	//if(mBottom < mViewport->getTop() + mViewport->getHalfHeight() ) return false;

	return true;
}




bool WDSObject::Move(float pX,float pY)
{
	m_PreviousPos = m_Pos;
	
	// check game boundary
	//if(getLeft()+pX >= 0 && getRight()+pX <= mLimitX  )
	//	setX(getX()+pX);
	//if(getTop()+pY >= 0 && getBottom()+pY <= mLimitY)
	//	setY(getY()+pY);
	
	for(int i =0; i < (int)mNeighbourList.size(); i++)
	{
		mCurrentCollideObject = NULL;
		if(mNeighbourList[i]->getIsAlive()== false )
			break;

		// hit with another object?
		//if(hitObjectAABB.Intersect(&myAABB))
		{
			mCurrentCollideObject = mNeighbourList[i];			
		}					

				
		if(isKillableType( mNeighbourList[i]->getType()))
		{
			//hitObjectAABB.SetRadius(mNeighbourList[i]->getX(),mNeighbourList[i]->getY(),mNeighbourList[i]->getHalfWidth()/2);
		}
		else
		{
			//hitObjectAABB.Set(mNeighbourList[i]->getLeft(),mNeighbourList[i]->getTop(),mNeighbourList[i]->getRight(),mNeighbourList[i]->getBottom());
		}

		//hitting something?
		//if(hitObjectAABB.Intersect(&myAABB) )
		{
		    //float tx = getHalfWidth()   * sin(mRotation);
			//float ty = getHalfHeight() * cos(mRotation);
			
			//myAABB.SetRadius( tx ,ty,mWidth );
			//if(hitObjectAABB.Intersect(&myAABB) == false)
			{
				// can't move
				// chit: should I bounce back?
				m_Pos = m_PreviousPos;
				i = mNeighbourList.size();	
				return false;
			}
			
		}		
	}
	return true;
	
}

WDSObject*  WDSObject::getCurrentCollideObject() const
{
	if(mCurrentCollideObject == NULL)
		return NULL;
	else
		return mCurrentCollideObject;
}


void WDSObject::addCollideableType(OBJECTTYPE pObjectType)
{
	this->mCollideableTypeList.insert(pObjectType);

}

bool WDSObject::isCollideableType(int pObjectType)
{
	if(mCollideableTypeList.find(pObjectType) != mCollideableTypeList.end())
		return true;
	else
		return false;
}

void WDSObject::addKillableType(int pObjectType)
{
	this->mKillableTypeList.insert(pObjectType);

}

bool WDSObject::isKillableType(int pObjectType)
{
	if(mKillableTypeList.find(pObjectType) != mKillableTypeList.end())
		return true;
	else
		return false;
}


void	WDSObject::hit(WDSObject* pCObject) 
{ 
	mNeighbourList.push_back(pCObject); 
}

void	WDSObject::clearNeighborObjectList()
{
	mNeighbourList.clear();		
}
